﻿using MeterReader.Models;
using MeterReader.PubSubEvents;
using MeterReader.Services;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System.Collections.ObjectModel;

namespace MeterReader.ViewModels
{
    public class MetersCurveViewModel : BindableBase
    {
        private readonly IDataService _dataService;
        private readonly IEventAggregator _eventAggregator;

        public MetersCurveViewModel(IDataService dataService, IEventAggregator eventAggregator)
        {
            _dataService = dataService;
            _eventAggregator = eventAggregator;
            MeterDataCollections = _dataService.GetMeterDataCollections();
            GoBackDataMonitorCommand = new DelegateCommand<string>(GoBackDataMonitor);
            OpenPopupCommand = new DelegateCommand<MeterDataCollection>(OpenPopup);
        }

        #region properties

        public ObservableCollection<MeterDataCollection> MeterDataCollections { get; set; } = new();

        private bool _isPopupOpen = false;

        public bool IsPopupOpen
        {
            get { return _isPopupOpen; }
            set { SetProperty(ref _isPopupOpen, value); }
        }

        private MeterDataCollection _popupMeterDatas;

        public MeterDataCollection PopupMeterDatas
        {
            get { return _popupMeterDatas; }
            set { SetProperty(ref _popupMeterDatas, value); }
        }

        public DelegateCommand<string> GoBackDataMonitorCommand { get; set; }
        public DelegateCommand<MeterDataCollection> OpenPopupCommand { get; set; }

        #endregion properties

        private void GoBackDataMonitor(string obj) => _eventAggregator.GetEvent<NavigateEvent>().Publish(obj);

        private void OpenPopup(MeterDataCollection collection)
        {
            if (collection != null)
            {
                PopupMeterDatas = collection;
                IsPopupOpen = true;
            }
        }
    }
}